package flyweight;

import java.util.HashMap;

public class FlyweightFactory {
    // 池子
    private static HashMap<String,Flyweight> pool = new HashMap<>();

    // 获取共享对象
    public static Flyweight getFlyweight(String extrinsic){
        // 返回共享对象
        Flyweight flyweight = null;
        // 池中没有该对象
        if(pool.containsKey(extrinsic)){
            flyweight = pool.get(extrinsic);
        }else{
            // 根据外部状态创建享元对象
            flyweight = new ConcreteFlyweight(extrinsic);
            // 回收到池中
            pool.put(extrinsic,flyweight);
        }
        return flyweight;
    }

    public static void show(){
        System.out.println("当前共享对象数：" + pool.size());
    }

}
